//app.js
var tabbarTemplate = require('components/tabBar/index.js');
App({
  onLaunch: function() {
    var that = this
    //获取设备信息
    wx.getSystemInfo({
      success: function(res) {
        that.globalData.statusBarHeight = res.statusBarHeight
        that.globalData.screenWidth = res.screenWidth * 2
      },
    })
    //检测token
    const token = wx.getStorageSync(that.globalData.tokenKey) || ''
    const userType = wx.getStorageSync(that.globalData.userTypeKey) || ''
    if (token == '' || userType == '') {
      wx.reLaunch({
        url: 'pages/logins/role/index',
      })
    } else {
      that.globalData.token = token
      that.globalData.userType = userType
      that.globalData.userData = wx.getStorageSync(that.globalData.userDataKey) || {}
      console.log(token)
      if (userType == '1') {
        that.globalData.courtId = that.globalData.userData.courtId
        that.globalData.courtName = that.globalData.userData.court.name
        that.globalData.userTypeStr = 'app_lawyer'
        wx.reLaunch({
          url: 'pages/index/judge/index',
        })
      } else {
        that.globalData.userTypeStr = 'app_plaintiff'
        wx.reLaunch({
          url: 'pages/index/people/index',
        })
      }
    }
    // 登录
    wx.login({
      success: res => {
        console.log(res.code)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    screenWidth: 0, //屏幕长度
    statusBarHeight: 20, //状态栏高度
    serverApiUrl: 'http://api.fayizxt.com:9101/',
    successCode: 200,
    failureCode: 2,
    tokenTimeOutCode: 250,
    resourcesServerURLs: 'https://nail-mobile.oss-cn-qingdao.aliyuncs.com',
    dbCollection: 'ReserveLookStyleManager',
    tokenKey: 'ReserveLookStyleManagerToken',
    userTypeKey: 'ReserveLookStyleManagerUserType',
    userDataKey: 'ReserveLookStyleManagerUserData',
    openid: '',
    wxUserInfo: null,
    userData: null,
    token: '',
    userType: '',
    userTypeStr: '',
    isLogin: false,
    counterId: '',
    newCount: 1,
    courtName: '全部法院',
    courtId: null,
    courtList: [], //法院列表
    areaList: [], //地区列表
    peopleLivehoodList:[],//民生类型列表
    auctionFilterOptionList: [], //标的物类型
    tabbarTemplate: tabbarTemplate
  },
  getServerUrl: function(url) {
    return this.globalData.serverApiUrl + url
  },
  /**
   * 获取上传服务器api地址
   */
  getUploadServerUrl(){
    return 'http://47.97.204.88:9201/fileUpload/uploadFile'
  },
  /**
   * 获取图片资源地址
   */
  resourcesFullUrl: function(url) {

    if (url.indexOf('http') != -1) {
      return url
    } else {
      return this.globalData.resourcesServerURLs + url
    }
  },
  /**
   * 获取加水印的图片地址
   */
  resourcesFullUrlWatermark: function(url) {
    if (url.indexOf('http') != -1) {
      return url + "?x-oss-process=style/mystyle"
    } else {
      return resourcesServerURLs + url + "?x-oss-process=style/mystyle"
    }
  },
  /**
   * 根据URL地址获取content-type类型
   * @url api请求地址
   */
  getContentType: function(url) {
    switch (url) {
      case "auction/subjectSubscribe/modify":
        return "application/json"
        break
      default:
        return "application/x-www-form-urlencoded"
        break
    }
  },
  /** 
   * 自定义post函数，返回Promises
   * @param {String}      url 接口网址
   * @param {arrayObject} data 要传的数组对象 例如: {name: 'aaa', age: 1}
   * @return {Promise}    promise 返回promise供后续操作
   */
  postJSON: function(url, data) {
    var that = this
    return new Promise((resolve, reject) => {
      //init
      //const fullUrl = this.globalData.serverApiUrl + url
      //网络请求
      wx.request({
        url: that.getServerUrl(url),
        data: data,
        method: 'POST',
        header: {
          'content-type': this.getContentType(url),
          'token': that.globalData.token,
          "type": that.globalData.userTypeStr
        },
        success: function(res) { //服务器返回数据
          if (res.data.code == that.globalData.tokenTimeOutCode) {
            wx.reLaunch({
              url: '/pages/logins/role/index'
            })
            return
          }
          resolve(res.data)
        },
        error: function(e) {
          reject({
            "status": 1,
            "code": 0,
            "msg": "网络出错",
            "data": {},
            "success": true
          });
        }
      })
    });
  }
})